#!/usr/bin/env python

import checkpointutils as ck
import sys
import os
import subprocess as sb
import vm
import nodeutils as nu

if len(sys.argv) < 2:
	print 'Wrong usage. Must give arguments 1->enable ping, 0->kill PM'
else:
	if int(sys.argv[1]) == 0:
		list = nu.EnumVMDomIDs()
		print list
		uuid = []
		for i in list:
			vms = nu.BuildVMObj(i)
			uuid = str(vms.domainCfgVars['uuid'])
			#p_old = sb.Popen('ls /vm/nilfsimg/' + uuid + '/logfile*', shell=True, stdout=sb.PIPE)
		        #oldLogFile = str(p_old.communicate()[0])
		        #oldLogFile = '/vm/nilfsimg/' + uuid + '/' + oldLogFileName     
		        #print 'Current (out of sync) image is ' + oldLogFile

			"""
			os.system('xm destroy ' + str(i))
			print 'Domain ' + str(i) + 'Destroyed'
			os.system('umount /vm/mnt/' + uuid + '/')
			print 'Unmounted'
		
			loopdev = str(ck.GetLoopDev(vms))

			cmd = '/sbin/losetup -d ' + loopdev
			print cmd
			
			p_detatch = sb.Popen('%s' %cmd, shell=True)
			p_detatch.wait()
			print 'Loop device detached'
			"""
		p_kill_pm = sb.Popen ('echo 1 >/proc/sys/net/ipv4/icmp_echo_ignore_all', shell=True)
		print 'Ping disabled. PM killed' 
			
	else:
		p_enable_pm = sb.Popen ('echo 0 >/proc/sys/net/ipv4/icmp_echo_ignore_all', shell=True)
		print 'Ping enabled. PM brought back from the dead!'
